package com.casic.ops.utils.pipeline;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.casic.ops.entity.domain.pipeline.PipelineDO;
import com.casic.ops.entity.dto.pipeline.PipelineDTO;
import com.casic.ops.entity.dto.pipeline.datastructure.graph.Graph;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 流水线转换类
 * @author cww
 * @date 2022/6/17
 */
@Component
public class PipelineConvertor {

    public PipelineDO toDO(PipelineDTO pipelineDTO){
        PipelineDO pipelineDO=new PipelineDO();
        pipelineDO.setPipelineName(pipelineDTO.getPipelineName());
        pipelineDO.setCreator(pipelineDTO.getCreator());
        pipelineDO.setDeleted(pipelineDTO.getDeleted());
        pipelineDO.setDescription(pipelineDTO.getDescription());
        pipelineDO.setId(pipelineDTO.getId());
        /**
         * 将Graph对象转换成String类型，便于数据库保存
         */
        String pipelineContext= JSONObject.toJSONString(pipelineDTO.getGraph());
        pipelineDO.setPipelineContext(pipelineContext);
        pipelineDO.setApplicationUuid(pipelineDTO.getApplicationUuid());
        pipelineDO.setGmtCreate(pipelineDTO.getGmtCreate());
        pipelineDO.setGmtModified(pipelineDTO.getGmtModified());
        pipelineDO.setTriggerWay(pipelineDTO.getTriggerWay());
        pipelineDO.setModifier(pipelineDTO.getModifier());
        pipelineDO.setBranch(pipelineDTO.getBranch());
        return pipelineDO ;
    };

    public PipelineDTO toDTO(PipelineDO pipelineDO){
        PipelineDTO pipelineDTO=new PipelineDTO();
        /**
         * 将数据库的String 类型的context转换成Graph对象
         */
        Graph graph=JSONObject.parseObject(pipelineDO.getPipelineContext(),Graph.class);
        pipelineDTO.setGraph(graph);
        pipelineDTO.setPipelineContext(pipelineDO.getPipelineContext());
        pipelineDTO.setDescription(pipelineDO.getDescription());
        pipelineDTO.setApplicationUuid(pipelineDO.getApplicationUuid());
        pipelineDTO.setCreator(pipelineDO.getCreator());
        pipelineDTO.setPipelineName(pipelineDO.getPipelineName());
        pipelineDTO.setTriggerWay(pipelineDO.getTriggerWay());
        pipelineDTO.setDeleted(pipelineDO.getDeleted());
        pipelineDTO.setId(pipelineDO.getId());
        pipelineDTO.setUuid(pipelineDO.getUuid());
        pipelineDTO.setModifier(pipelineDO.getModifier());
        pipelineDTO.setApplicationUuid(pipelineDO.getApplicationUuid());
        pipelineDTO.setGmtCreate(pipelineDO.getGmtCreate());
        pipelineDTO.setGmtModified(pipelineDO.getGmtModified());
        return pipelineDTO;
    }

    public List<PipelineDTO> toDTOList(List<PipelineDO> list){
        List<PipelineDTO> pipelineDTOList=new ArrayList<>();
        for(PipelineDO pipelineDo:list){
            pipelineDTOList.add(toDTO(pipelineDo));
        }
        return pipelineDTOList;
    }

    public List<PipelineDO> toDOList(List<PipelineDTO> list){
        List<PipelineDO> pipelineDOList=new ArrayList<>();
        for(PipelineDTO pipelineDTO:list){
            pipelineDOList.add(toDO(pipelineDTO));
        }
        return pipelineDOList;

    }



}
